package com.xique.door.biz.service.impl;


import com.xique.common.core.text.Convert;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.domain.PermissionGroupUser;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.bean.request.commandV1.DeviceCommandUserInfoDelRequest;
import com.xique.door.bean.request.commandV1.DeviceCommandUserInfoDownloadRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeleteUserRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupSelectUserRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupUserRequest;
import com.xique.door.bean.search.PermissionGroupSelectUserSearch;
import com.xique.door.bean.search.UserDeviceSearch;
import com.xique.door.biz.service.IDeviceCommandNewService;
import com.xique.door.biz.service.IPermissionGroupUserBizService;
import com.xique.door.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
@Deprecated
public class PermissionGroupUserBizServiceImpl implements IPermissionGroupUserBizService {

    @Autowired
    private IPermissionGroupUserService permissionGroupUserService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Autowired
    private IPermissionGroupUserDomainService permissionGroupUserDomainService;

    @Autowired
    private IDeviceCommandNewService deviceCommandNewService;

    @Autowired
    private IPermissionGroupDeviceService permissionGroupDeviceService;

    @Override
    public AjaxResult deleteUser(PermissionGroupDeleteUserRequest request) {

        Long groupId = request.getGroupId();
        Long userId = request.getUserId();

        // 1.删除权限组用户
        PermissionGroupSelectUserSearch search = new PermissionGroupSelectUserSearch();
        search.setGroupId(groupId);
        search.setUserIds(Collections.singletonList(userId));
        List<PermissionGroupUser> permissionGroupUserList = permissionGroupUserService.selectPermissionGroupUserListBySearch(search);
        Long permissionGroupUserId = permissionGroupUserList.stream().map(PermissionGroupUser::getId).collect(Collectors.toList()).get(0);

        // 2.删除用户设备表信息
        UserDeviceSearch userDeviceSearch = new UserDeviceSearch();
        userDeviceSearch.setGroupId(groupId);
        userDeviceSearch.setUserId(userId);
        List<UserDevice> userDeviceList = userDeviceService.selectUserDeviceListBySearch(userDeviceSearch);
        List<Long> userDeviceIds = userDeviceList.stream().map(UserDevice::getId).collect(Collectors.toList());

        // 3.下发删除信息到设备
        DeviceCommandUserInfoDelRequest delRequest = new DeviceCommandUserInfoDelRequest();
        delRequest.setGroupId(groupId);
        delRequest.setUserId(userId);
        delRequest.setAsync(true);
        AjaxResult result = deviceCommandNewService.permissionUserInfoDel(delRequest);
        if (!AjaxResult.isSuccess(result)) {
            return AjaxResult.error("删除失败");
        }

        boolean isSuccess = permissionGroupUserDomainService.deleteUserInfo(permissionGroupUserId, userDeviceIds);
        if (!isSuccess) {
            return AjaxResult.error("删除失败");
        }

        return result;
    }

    /**
     * 批量删除权限组用户
     * @author shd
     * @date 2021/7/7 11:13
     * @param request 入参
     * @return com.ruoyi.common.core.domain.AjaxResult
     * 1.删除权限组用户 2.删除用户设备表信息 3.下发用户删除信息到设备
     */
    @Override
    public AjaxResult deleteUsers(PermissionGroupDeleteUserRequest request) {

        Long groupId = request.getGroupId();
        List<Long> userIds = Arrays.asList(Convert.toLongArray(request.getUserIds()));

        // 1.删除权限组用户
        PermissionGroupSelectUserSearch search = new PermissionGroupSelectUserSearch();
        search.setGroupId(groupId);
        search.setUserIds(userIds);
        List<PermissionGroupUser> permissionGroupUserList = permissionGroupUserService.selectPermissionGroupUserListBySearch(search);
        List<Long> permissionGroupUserIds = permissionGroupUserList.stream().map(PermissionGroupUser::getId).collect(Collectors.toList());

        // 2.删除用户设备表信息
        UserDeviceSearch userDeviceSearch = new UserDeviceSearch();
        userDeviceSearch.setGroupId(groupId);
        userDeviceSearch.setUserIds(userIds);
        List<UserDevice> userDeviceList = userDeviceService.selectUserDeviceListBySearch(userDeviceSearch);
        List<Long> userDeviceIds = userDeviceList.stream().map(UserDevice::getId).collect(Collectors.toList());

        // 3.下发删除信息到设备
        DeviceCommandUserInfoDelRequest delRequest = new DeviceCommandUserInfoDelRequest();
        delRequest.setGroupId(groupId);
        delRequest.setUserIds(userIds);
        delRequest.setAsync(true);
        deviceCommandNewService.permissionUserInfosDel(delRequest);

        boolean isSuccess = permissionGroupUserDomainService.deleteUserInfos(permissionGroupUserIds, userDeviceIds);
        if (!isSuccess) {
            return AjaxResult.error("删除失败");
        }

        return AjaxResult.success("删除完成");
    }

    /**
     * 批量添加权限组用户
     * @author shd
     * @date 2021/7/8 14:56
     * @param request
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult addUsers(PermissionGroupSelectUserRequest request) {

        Long groupId = request.getGroupId();
        List<Long> userIds = Arrays.asList(Convert.toLongArray(request.getUserIds()));

        PermissionGroupSelectUserSearch search = new PermissionGroupSelectUserSearch();
        search.setGroupId(groupId);
        search.setUserIds(userIds);
        List<PermissionGroupUser> existGroupUserList = permissionGroupUserService.selectPermissionGroupUserListBySearch(search);
        if (CollectionUtils.isNotEmpty(existGroupUserList)) {
            List<Long> existUserIds = existGroupUserList.stream().map(PermissionGroupUser::getUserId).collect(Collectors.toList());
            userIds = userIds.stream().filter(item -> !existUserIds.contains(item)).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(userIds)) {
            return AjaxResult.success();
        }

        List<PermissionGroupUser> groupUserList = new ArrayList<>();
        userIds.forEach(item -> {
            PermissionGroupUser groupUser = new PermissionGroupUser();
            groupUser.setGroupId(groupId);
            groupUser.setUserId(item);
            groupUserList.add(groupUser);
        });

        List<PermissionGroupDevice> deviceList = permissionGroupDeviceService.selectPermissionGroupDeviceListByGroupIds(Collections.singletonList(groupId));

        List<UserDevice> list = new ArrayList<>();
        userIds.forEach(item -> {
            deviceList.forEach(device -> {
                UserDevice userDevice = new UserDevice();
                userDevice.setParkId(request.getParkId());
                userDevice.setGroupId(groupId);
                userDevice.setUserId(item);
                userDevice.setDeviceId(device.getDeviceId());
                userDevice.setDevSn(device.getDevSn());
                list.add(userDevice);
            });
        });

        DeviceCommandUserInfoDownloadRequest delRequest = new DeviceCommandUserInfoDownloadRequest();
        delRequest.setGroupId(groupId);
        delRequest.setUserIds(userIds);
        delRequest.setAsync(true);
        deviceCommandNewService.userInfosDownload(delRequest);

        boolean isSuccess = permissionGroupUserDomainService.addUserInfos(groupUserList, list);
        if (!isSuccess) {
            return AjaxResult.error();
        }

        return AjaxResult.success();
    }

    @Override
    public List<PermissionGroupUser> selectAllocatedUserList(PermissionGroupUserRequest request) {
        return permissionGroupUserService.selectAllocatedUserList(request);
    }

    @Override
    public List<PermissionGroupUser> selectUnallocatedUserList(PermissionGroupUserRequest request) {
        return permissionGroupUserService.selectUnallocatedUserList(request);
    }
}
